<template>
<div>
  <div style="padding: 0px 15px;" class="overflowY">
    <div class="searchGroup_list" style="padding: 0px 0px 10px 0px;margin: 0px 0px 0px 0px;">
      <el-row :gutter="20">
          <div class="searchBoxList" style="display: flex;padding: 0 10px">
            <div style="width: 85px;">
              <span class="searchListName">菜单名称: </span>
            </div>
            <div style="width: 30%;">
              <el-input size="small" v-model="queryCondition.title" placeholder="菜单名称" :clearable="true"></el-input>
            </div>
            <div style="width: 100px;">
              <span class="searchListName">上级菜单名称: </span>
            </div>
            <div style="width: 30%;">
              <el-input size="small" v-model="queryCondition.parentName" placeholder="上级菜单名称" :clearable="true"></el-input>
            </div>
          </div>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <div style="display: inline-block;text-align: left;width: 100%;padding-top: 11px;">
            <el-button type="primary" size="mini" class="size" icon="el-icon-plus" @click="setMenuSkip('save')">添加</el-button>
          </div>
        </el-col>
        <el-col :span="12">
          <div style="display: inline-block;text-align: right;width: 100%;padding-top: 11px; padding-right: 5px">
            <el-button type="primary" size="mini" class="size" icon="el-icon-search" @click="fetchData">搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div style="position: relative;border: 1px solid #ccc;padding:0px 0px 54px 0px;margin:0px 0px 0px 0px;position: relative;">
      <el-table
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        tooltip-effect="dark"
        @select="select"
        @select-all="selectalldate"
        @row-contextmenu="rowContextmenu"
        @cell-dblclick="cellClick"
        @row-click="rowClick"
        @sort-change="sortChange"
        @current-change="handleCurrentChange"
        height="340"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          width="50"
          align="center"
          label="序号">
          <template slot-scope="scope">
            <span>{{scope.$index + (pagingArgs.currentPageNo - 1) * 10 + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="260"
          prop="title"
          align="left"
          sortable="custom"
          :show-overflow-tooltip="true"
          label="菜单名称">
        </el-table-column>
        <el-table-column
          width="250"
          prop="parentName"
          align="left"
          sortable="custom"
          :show-overflow-tooltip="true"
          label="上级菜单名称">
        </el-table-column>
      </el-table>
      <div style="position: absolute;bottom:0;left: 10px;">
        <paging :size-change="pageSizeChange"
                :current-change="pagingQuery"
                :page-sizes="pageSizes"
                :total-num="pagingArgs.totalCount">
        </paging>
      </div>
    </div>
  </div>
</div>
</template>

<script>
  import paging from '@/components/demoAssembly/paging' // 分页

  export default {
    name: 'supplierList',
    props: ['tableSelectData'],
    data() {
      return {
        pageSizes: [10, 20, 30, 40],
        // 分页参数
        pagingArgs: {
          currentPageNo: 1,
          totalCount: 0
        },
        tableData: [], // 列表数据
        queryArgs: {
          sort: [
            {
              property: 'sort',
              direction: 'ASC'
            }
          ],
          rowBounds: {
            offset: 0,
            limit: 10
          }
        },
        userInfo: {
          userId: this.$store.state.user.userid,
          userName: this.$store.state.user.name,
          userOrgId: this.$store.state.user.orgId,
          userOrgName: this.$store.state.user.org_name,
          userCorpId: this.$store.state.user.corpId,
          userCorpName: this.$store.state.user.corpName,
          email: this.$store.state.user.email,
          remarks: this.$store.state.user.remarks
        },
        // 查询参数
        queryCondition: {
          role_status: '0',
          user_id:this.$store.state.user.userid,
          type: '0',
          UtilitiesIsUse: '1'
        },
        recordId: '',
        openType: '',
        selectRecord: null,
        dialogVisible: false,
        supplierData: []
      }
    },
    watch: {
      // 普通的watch监听通的watch监听
    },
    computed: {
    },
    filters: {

    },
    components: {
      paging
    },
    /**
     * 组件创建完成后，查询列表数据
     */
    created() {

    },
    mounted() {

      this.fetchData()
    },
    methods: {
      rowClick(row, column, event) {
        // 行单击事件
        // console.log(row, event, column)
        // this.supplierData.companyName = row.companyName
        // this.supplierData.mdmSupplierId = row.mdmSupplierId
      },
      select(selection, row) {
        this.supplierData = selection
      },
      selectalldate(selection){
        this.supplierData = selection
      },
      cellClick(row, column, cell, event) {
        // 列单击事件
      },
      rowContextmenu(row, column, e) {
        // 右键菜单
        window.event.returnValue = false
      },
      pageSizeChange(val) {
        this.queryArgs.rowBounds.limit = val
        this.queryArgs.rowBounds.offset = 0
        this.pagingArgs.currentPageNo = 1
        this.fetchData()
      },
      sortChange(col) {
        //
        if (col.prop) {
          let tempDirection = 'ASC'
          if (col.order === 'descending') {
            tempDirection = 'DESC'
          }
          const sort = {
            property: col.prop,
            direction: tempDirection
          }
          this.queryArgs.sort = []
          this.queryArgs.sort.push(sort)
          this.fetchData()
        }
      },
      pagingQuery(val) { // 分页
        this.queryArgs.rowBounds.offset = (val - 1) * 10
        this.pagingArgs.currentPageNo = val
        this.fetchData()
      },
      buildQueryConditions() {
        let tempQueryCondition = []
        for (let key in this.queryCondition) {
          let queryValue = this.queryCondition[key]
          if (queryValue != null && queryValue !== '') {
            let type = 'LIKE'
            if (key === 'role_status' || key === 'user_id' || key === 'type' || key === 'UtilitiesIsUse') {
              type = 'EQ'
            }
            let query = {
              property: key,
              value: queryValue,
              type: type
            }
            tempQueryCondition.push(query)
          }
        }
        return tempQueryCondition
      },
      clearQueryConditions() {
        for (let key in this.queryCondition) {
          this.queryCondition[key] = ''
        }
      },
      fetchData() { // 获取内容
        this.dialogVisible = true
        this.queryArgs.condition = this.buildQueryConditions()
        this.$store
          .dispatch('utilityUserMenuSelect', this.queryArgs)
          .then(res => {
            let records = res.data.records
            this.tableData = records
            this.$nextTick(() => {
              if(this.tableSelectData !== ''){
                const tableSelect = this.tableSelectData
                this.tableData.forEach((val) => {
                  tableSelect.forEach(valChild => {
                    if(val.id === valChild.id){
                      this.$refs.singleTable.toggleRowSelection(val, true)
                    }
                  })
                })
              }
            })
            this.pagingArgs.totalCount = res.data.count
            this.selectRecord = null
          }).catch(() => {
          })
      },
      handleCurrentChange(currentRow, oldCurrentRow) {
        //
        this.selectRecord = currentRow
      },
      setMenuSkip(type) {
        let tempQueryCondition = {
          userid: '',
          date:[]
        }
        tempQueryCondition.userid  = this.userInfo.userId
        tempQueryCondition.date  = this.supplierData
        // 插入
        this.$store.dispatch('utilitybatchByUserId', tempQueryCondition).then(res => {
          if (res.success) {
            this.$emit('UtilityUserMenusavesuccess')
          }
          this.saveCallBack(type, res)
        }).catch((err) => {
          console.log(err)
          this.saveErrorCallBack(type, err)
        })


        // this.$emit('customerinfo', this.supplierData)
        // this.$emit('changeCustomerCloseDialog')
      },
      saveErrorCallBack(type, err) {
        let msg = '服务器连接异常，保存失败!'
        if (type === 'submit') {
          msg = '服务器连接异常，提交失败!'
        }
        this.$message({
          type: 'danger',
          message: msg
        })
      },
      saveCallBack(type, res) {
        if (type === 'submit') {
          if (res && res.success) {
            this.record = res.data
            this.corpName = this.record.companyName
            this.doSubmitBefore()
          } else {
            this.$message({
              type: 'danger',
              message: '提交时，保存业务数据失败!' + res.message
            })
          }
        } else {
          if (res && res.success) {
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.record = res.data
          } else {
            this.$message({
              type: 'danger',
              message: '保存失败!' + res.message
            })
          }
        }
      },
      close() {
        this.$emit('changeUtilityCloseDialog')
      }
    }
  }
</script>
<style scoped>
  .tree-title{
    font-size: 16px;
    padding:10px 10px 10px 20px;
    border-bottom: 1px solid #ccc;
  }
  .size{
    padding:4px;
  }
  .tree-detail{
    padding: 10px 0 80px 15px;
  }
  .notSize .el-table__empty-text{
    display: none;
  }
  .tableBtnBox >>> .el-button.is-circle{
    padding: 8px !important;
  }

  .searchBoxList{
    margin-top: 10px;
  }
  .searchGroup_list .searchListName{
    font-size: 14px;
    color: #333333;
    display: inline-block;
    vertical-align: middle;
    line-height: 32px;
  }
  .searchGroup_list .searchListNameOther{
    font-size: 14px;
    color: #333333;
    display: inline-block;
    vertical-align: middle;
    line-height: 32px;
    text-align: center;
    padding-left: 20%;
    padding-right: 20%;
  }

  .tableBtnBox >>> .el-button.is-circle {
    padding: 4px !important;
  }
  .tableBtnBox >>> .el-button+.el-button {
    margin-left: 5px;
  }
</style>
